<?php
namespace app\middleware;
//引用的JWT

class AuthAdmin
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next){
           try {            
            $header = $request->header(); // token 合法          
            $account = isset($header['account'])?$header['account']:'';
            $signature = isset($header['signature'])?$header['signature']:'';
            // $redis=\think\facade\Cache::store('redis')->get('USER_'.$account);
            $redis=\think\facade\Cache::store('redis')->get('USER_'.$account.'_'.$signature);
            $redis=json_decode(json_encode($redis),true);
            if(!$redis){
                return json(['code'=>201,'success'=>false,'message'=>'有效时间失效']);
            }
           
            // return json($redis);
            if($redis['signature']!=$signature){
                return json(['code'=>201,'success'=>false,'message'=>'有效时间失效']);
            }
      
        }catch (Exception $e){            
            return json(['code'=>201,'success'=>false,'message'=>'有效时间失效','msg'=>$e]);       
        }
        return $next($request); 
    }
}
